package com.sdp.wx.flow;

import java.lang.reflect.Method;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;

import com.sdp.core.bizc.exception.ApplicationException;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.flow.utils.FlowConstants;
import com.sdp.wx.utils.WxConstants;

public class ApproveListener implements ExecutionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void notify(DelegateExecution execution) {
		try {
			Object approverNo = execution.getVariable(FlowConstants.FLOW_ASSIGN_PERSON_KEY);
			execution.setVariable(FlowConstants.FLOW_ASSIGN_PERSON_KEY,approverNo);
			Object classNameObj = execution.getVariable(WxConstants.流程监听器中执行的类.getCode());
			Object methodNameObj = execution.getVariable(WxConstants.流程监听器中执行类的方法.getCode());
			Object param = execution.getVariable(WxConstants.流程监听器中执行类的方法的参数.getCode());
			if(classNameObj!=null && methodNameObj!=null) {
				if(param==null) {
					param = new BusinessContext();
				}
				Class<?> c = Class.forName(classNameObj.toString());
				Method method= c.getDeclaredMethod(methodNameObj.toString(),BusinessContext.class);
				method.invoke(ApplicationContextUtils.getBean(c),param);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException(e.getMessage());
		}
		
	}

}
